package com.duyi.demo.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @author ctx
 * @date 2020/2/12
 * @description
 */
public class HttpClientUtil {
//    public static void main(String[] args) {
//        String str = doGet("https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5");
//        System.out.println(str);
//    }
    public static String doGet(String url){
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;

        String result = "";
        try{
            // 通过默认配置 创建一个实例
            httpClient = HttpClients.createDefault();
            //创建一个远程连接的实例
            HttpGet httpGet = new HttpGet(url);
            //设置请求头
            httpGet.setHeader("Accept","application/json");
            //配置请求参数
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(15000)
                    .setSocketTimeout(60000)
                    .build();
            //进行配置
            httpGet.setConfig(requestConfig);
            //执行请求
            response = httpClient.execute(httpGet);

            //接受返回的数据
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(response != null){
                    response.close();
                }
                if(httpClient != null){
                    httpClient.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return result;

    }
}
